package com.bjpowernode.controller;

import com.bjpowernode.pojo.Admin;
import com.bjpowernode.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/admin")
public class AdminAction {
    //在所有的界面层，一定会有业务逻辑层的对象
    @Autowired
    AdminService adminService;

    //实现登录判断，并进行相应的跳转
    @RequestMapping("/login")
    public String login(String name, String pwd, HttpServletRequest request) {

        Admin admin = adminService.login(name,pwd);
        if (admin != null) {
            //登录成功
            request.setAttribute("admin",admin);
            return "main";
        }else {
            //登录失败
            request.setAttribute("errmsg","用户名或密码不正确！");
            return "login";
        }
    }

    @RequestMapping("/regist")
    public String regist() {
        return "regist";
    }

    @RequestMapping("/out")
    public String out() {
        return "login";
    }

    @RequestMapping("/saveAdmin")
    public String saveAdmin(String myname, String mypwd, HttpServletRequest request) {
        Admin admin = new Admin();
        admin.setaName(myname);
        admin.setaPass(mypwd);
        int num = -1;
        try{
            num = adminService.insert(admin);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (num > 0) {
            request.setAttribute("errmsg", "注册成功！");
        }else {
            request.setAttribute("errmsg", "注册失败！");
        }
        return "login";
    }
}
